#include "core/Application.h"
#include "core/Logger.h"

#include <iostream>
#include <string>

void printUsage(const char* programName) {
    std::cout << "Usage: " << programName << " [OPTIONS]\n"
              << "Options:\n"
              << "  --config PATH     Configuration directory path (default: ./config)\n"
              << "  --help           Show this help message\n"
              << "  --version        Show version information\n"
              << "  --status         Show application status\n"
              << std::endl;
}

void printVersion() {
    std::cout << "EMS-CPP v1.0.0\n"
              << "Lightweight Energy Management System\n"
              << "Copyright (c) 2024 EMS Project\n"
              << std::endl;
}

int main(int argc, char* argv[]) {
    std::string configPath = "./config";
    
    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        
        if (arg == "--help") {
            printUsage(argv[0]);
            return 0;
        } else if (arg == "--version") {
            printVersion();
            return 0;
        } else if (arg == "--config") {
            if (i + 1 < argc) {
                configPath = argv[++i];
            } else {
                std::cerr << "Error: --config requires a path argument" << std::endl;
                return 1;
            }
        } else if (arg == "--status") {
            std::cout << "Application status checking not implemented yet" << std::endl;
            return 0;
        } else {
            std::cerr << "Error: Unknown option: " << arg << std::endl;
            printUsage(argv[0]);
            return 1;
        }
    }
    
    try {
        auto& app = ems::core::Application::getInstance();
        
        if (!app.initialize(configPath)) {
            std::cerr << "Failed to initialize application" << std::endl;
            return 1;
        }
        
        // 启动应用（已包含信号处理）
        app.run();
        
    } catch (const std::exception& e) {
        std::cerr << "Application error: " << e.what() << std::endl;
        return 1;
    } catch (...) {
        std::cerr << "Unknown application error" << std::endl;
        return 1;
    }
    
    return 0;
}